import Dialog from '@vant/weapp/dialog/dialog';
const app = getApp();
var url = app.data.url;
var map = {};

Page({

  /**
   * 页面的初始数据
   */
  data: {
    merchantId: '',
    type : '',
    requiredPersonNum : 1,
    show : false,
    deskSelectShow: false,
    typeColumns: ['大厅大桌子', '大厅小桌子', '大厅中桌子', '大房', '中房', '小房'],
    displayType: '',
    deskType: -1,
    totalNum: 1,
    personNum: 1,
    bookingTypeList: [],
    typeStr: '',
    delShow : false,
    delId : ''
  },

  onLoad: function (options) {
    var th = this;
    let merchantId = options.merchantId;
    
    th.setData({
      merchantId: merchantId
    })
    th.queryData(merchantId);
    
  },

  onActiveChange(event){
    let _that = this;
    _that.setData({
      active: event.detail
    });
  },

  openDeskPopup(option){
    let _that = this;
    let optype = option.currentTarget.dataset.optype;
    let type = option.currentTarget.dataset.type;
    let active = option.currentTarget.dataset.active;
    let id = option.currentTarget.dataset.id;
    if (optype == 1) {
      _that.setData({
        show: true,
        type: type,
        active: active == "Y" ? true : false,
        delShow: false,
        delId : id
      })
    } else {
      Dialog.alert({
        title: '稍等～～～',
        message: '暂不支持新增类型,请等待系统后续优化'
      }).then(() => {
        // on close
      });
    }
   

    /*let _that = this;
    let opType = option.currentTarget.dataset.opType;
    if (opType == 1) {
      let displayType = option.currentTarget.dataset.displaytype;
      let deskType = option.currentTarget.dataset.desktype;
      let totalNum = option.currentTarget.dataset.totalnum;
      let personNum = option.currentTarget.dataset.personnum;
      let id = option.currentTarget.dataset.id;
      console.log(option.currentTarget.dataset);
      console.log(id);
      console.log(deskType);
      console.log(totalNum);
      console.log(personNum);
      _that.setData({
        show: true,
        displayType: displayType,
        deskType: deskType,
        totalNum: totalNum,
        personNum: personNum,
        delShow : true,
        delId : id
      })
    } else {
      _that.setData({
        show: true,
        delShow: false,
        type : '',
        requiredPersonNum : 1
      })
    }*/
  },

  onClose(){
    let _that = this;
    _that.setData({
      show: false
    })
  },

  onTypeChange(option){
    let _that = this;
    _that.setData({
      type: option.detail
    })
  },

  openDeskSelect(){
    let _that = this;
    if (!_that.data.delShow) {
      //新增才可以弹出
      _that.setData({
        deskSelectShow: true
      })
    }
  },

  typeOptionConfirm(event){
    var th = this;
    var index = '';
    if ("大厅大桌子" == event.detail.value){
       index = '0';
    } else if ("大厅小桌子" == event.detail.value){
      index = '1';
    } else if ("大厅中桌子" == event.detail.value) {
      index = '2';
    } else if ("大房" == event.detail.value) {
      index = '3';
    } else if ("中房" == event.detail.value) {
      index = '4';
    } else if ("小房" == event.detail.value) {
      index = '5';
    }
    th.setData({
      deskSelectShow: false,
      displayType: event.detail.value,
      deskType: index
    });
    console.log(event.detail.index);
  },

  typeOptionCancel(){
    let _that = this;
    _that.setData({
      deskSelectShow: false
    })
  },

  onRequiredPersonNumChange(event){
    let _that = this;
    _that.setData({
      requiredPersonNum: event.detail
    })
  },

  onPersonNumChange(event){
    let _that = this;
    _that.setData({
      personNum: event.detail
    })
  },

  del(){
    var th = this;
    console.log(th.data.delId);
    console.log(th.data.deskType);
    wx.request({
      url: url + "/DeskApi/deleteDesk",
      data: { "id": th.data.delId},
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res);
        if (res.data.code == 200) {
          //将删除的餐桌类型放入选择框
          var array = th.data.typeColumns;
          array[array.length] = map[th.data.deskType];
          th.setData({
            show: false,
            typeColumns: array
          })
          th.queryData(th.data.merchantId);
          
          wx.showToast({
            title: "操作成功!",
            icon: "success",
            duration: 3000
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 3000
          })
        }
      },
      fail: function (res) {
        console.info('submit fail');
      },
      complete: function (res) {
        console.info('submit complete');
      }
    })
  },

  save(){
    var th = this;
    var type = th.data.type;
    //var requiredPersonNum = th.data.requiredPersonNum;
    var merchantId = th.data.merchantId;
    var active = th.data.active;
    var path = '';

    /*var msg = "";
    if (type == '') {
      msg = "请输入类型";
    } 

    if (msg != "") {
      Dialog.alert({
        title: '注意',
        message: msg
      }).then(() => {
        // on close
      });
      return false;
    }*/

    var tempData = {
      type: type,
      active : active ? 'Y' : 'N',
      //requiredPersonNum: requiredPersonNum,
      merchantId: merchantId
    }
    tempData.id = th.data.delId;
    path = '/BookingTypeApi/editBookingType'

    /*if (th.data.delShow) {
      //编辑
      path = '/BookingTypeApi/editBookingType'
      tempData.id = th.data.delId;
    } else {
      //新增
      path = '/BookingTypeApi/insertBookingType'
    }*/

    console.log(path);
    console.log(tempData);

    wx.request({
      url: url + path,
      data: tempData,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res);
        if (res.data.code == 200) {
          th.setData({
            show: false
          })
          th.queryData(merchantId);
          wx.showToast({
            title: "操作成功!",
            icon: "success",
            duration: 3000
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 3000
          })
        }
      },
      fail: function (res) {
        console.info('submit fail');
      },
      complete: function (res) {
        console.info('submit complete');
      }
    })

    /*var deskType = th.data.deskType;
    var totalNum = th.data.totalNum;
    var personNum = th.data.personNum;
    var merchantId = th.data.merchantId;
    var path = '';
    
    console.log(deskType);

    var msg = "";
    if (deskType == -1) {
      msg = "请选择餐桌类型";
    } 

    if (msg != "") {
      Dialog.alert({
        title: '注意',
        message: msg
      }).then(() => {
        // on close
      });
      return false;
    }

    var tempData = {
      deskType: deskType,
      totalNum: totalNum,
      personNum: personNum,
      merchantId: merchantId
    }
    console.log(th.data.delShow);
    if (th.data.delShow) {
      //编辑
      path = '/DeskApi/editDesk'
      tempData.id = th.data.delId;
    } else {
      //新增
      path = '/DeskApi/insertDesk'
    }
    console.log(path);
    wx.request({
      url: url + path,
      data: tempData,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res);
        if (res.data.code == 200) {
            th.setData({
              show: false
            })
          th.queryData(merchantId);
          wx.showToast({
            title: "操作成功!",
            icon: "success",
            duration: 3000
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 3000
          })
        }
      },
      fail: function (res) {
        console.info('submit fail');
      },
      complete: function (res) {
        console.info('submit complete');
      }
    })*/
  },

  queryData: function (merchantId) {
    let _that = this;
    wx.request({
      url: url + '/BookingTypeApi/listBookingType',
      data: {
        merchantId: merchantId,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.code == 200) {
          console.log(res.data.data);
          _that.setData({
            bookingTypeList: res.data.data
          })
          /*var array = _that.data.typeColumns;
          
          for (var k = 0; k < res.data.data.pageList.length; k++){
            for (var i = array.length - 1; i >= 0; i--) {
              if (array[i] == map[res.data.data.pageList[k].deskType]) {
                array.splice(i, 1);        //执行后aa.length会减一
              }
            }
            res.data.data.pageList[k].displayType = map[res.data.data.pageList[k].deskType];
          }

          console.log("剩下可选餐桌类型: " + array);
          
          _that.setData({
            deskList: res.data.data.pageList,
            typeColumns : array
          })*/
          
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }
      },
      fail: function (res) {
        console.info('submit fail');
      },
      complete: function (res) {
        console.info('submit complete');
      }
    })

  }

})